package org.openkanban.client.cometd;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * the use of this Client requires the dojo js toolkit (which could be included
 * with the script tag in the html file).
 * 
 * @author msa
 * 
 */
public class CometClient {

    public CometClient() {
	init();
    }

    public native void init() /*-{
			      $wnd.dojo.require("dojox.cometd");
			      }-*/;

    public native void connect(String email, String password) /*-{
							      $wnd.dojo.ready(function(){
							      $wnd.dojox.cometd.init("cometd", {ext:{authentication:{user:email,pwd:password}}});
							      
							      $wnd.dojox.cometd.subscribe("/meta", function(message){
							      });
							      });
							      }-*/;

    public native void disconnect() /*-{
				    $wnd.dojo.ready(function() {
				    $wnd.dojox.cometd.disconnect();
				    });
				    }-*/;

    public native void subscribe(String channel, SubscribeCallback subscribeCallback) /*-{
										      $wnd.dojo.ready(function(){
										      $wnd.dojox.cometd.subscribe(channel, function(message){
										      subscribeCallback.@org.openkanban.client.cometd.SubscribeCallback::execute(Lcom/google/gwt/core/client/JavaScriptObject;)(message);
										      });
										      });
										      }-*/;

    public native void publish(String channel, JavaScriptObject data) /*-{
								      $wnd.dojo.ready(function(){
								      $wnd.dojox.cometd.publish(channel, data);
								      });
								      }-*/;

}
